<?php

namespace app\command;

use app\model\DistributionSetting;
use app\model\Goods;
use app\model\GoodsInfo;
use app\model\Shop;
use app\service\GoodsService;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Log;
use util\Logger;
use Wechat\CommerceApp;

class GoodsTaskCommand extends Command
{
    protected function configure()
    {
        $this->setName('goods')
            ->setDescription('获取可推广的商品列表');
    }

    protected function execute(Input $input, Output $output)
    {
        Log::init([
            'type'        => 'file',
            'apart_level' => ['error'],
            'level'       => ['error'],
            'path'        => app()->getRootPath() . "runtime/log/goods/",
            'max_files'   => 5,
        ]);
        try {
            $next_key    = '';
            $setting     = DistributionSetting::getLevelInfo(1, 1);
            $share_ratio = $setting['ratio'] ?? 0;
            do {
                [$save, $shop_save, $info_save, $next_key, $sum] = GoodsService::getGoodsList(4, $share_ratio, $next_key);
                $sum += $sum;
                if ($save) {
                    (new Goods())->saveAll($save);
                }
                if ($shop_save) {
                    (new Shop())->saveAll($shop_save);
                }
                if ($info_save) {
                    (new GoodsInfo())->saveAll($info_save);
                }
                var_dump('已处理商品数：' . $sum);
            } while ($next_key);
            var_dump('总商品数：' . $sum);
        } catch (\Exception $e) {
            Log::error($e->getMessage());
            exit;
        }

    }
}
